package com.api.fc;


import com.alibaba.fastjson.JSONObject;
import com.api.workflow.util.DesUtils;
import com.google.gson.Gson;
import weaver.general.Util;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;

@Path("/fc_get_fj")
public class fc_get_fujian_url {

//    @GET
//    @Path("/geturl_by_imagefileid")
//    @Produces(MediaType.APPLICATION_JSON)
//    public String geturl_by_imagefileid(@Context HttpServletRequest request, @Context HttpServletResponse response) throws Exception {
//        String url=request.getParameter("url");//http://oa.maxwell-gp.com:8090
//        // /fc_get_fj/geturl_by_imagefileid
//        String imagefileids=request.getParameter("imagefileid");
//        String endString="[";
//
//        String imagefileid[]=imagefileids.split(",");
//        for( int i=0;i<imagefileid.length;i++){
//            String id=imagefileid[i];
//            String output = new DesUtils().encrypt(1 + "_"+id );
//            output=url+"/weaver/weaver.file.FileDownload?fileid="+id+"&download=1&ddcode="+output;
//            String line="{";
//            line+="\"imagefileid\":\""+id+"\"";
//            line+=",\"url\":\""+output+"\"";
//            line+="}";
//
//            if(endString.equals("[")){
//                endString+=line;
//            }else{
//                endString+=","+line;
//            }
//        }
//        endString+="]";
//        Map<String, Object> data = new HashMap<String, Object>();
//        data.put("result",endString);
//        String json = JSONObject.toJSONString(data);
//        return json;
//    }
//
//    //下面的这些没用上
//    @Path("/download_by_imagefileid")
//    @POST
//    @Produces(MediaType.APPLICATION_JSON)
//
//    public String download_by_imagefileid(@Context HttpServletResponse response, @Context HttpServletRequest request) throws Exception {
//
//        String fileid="558701";
//        String serverIp="http://192.168.0.16:8899/";
//        String downUrl = "http://192.168.0.16:8899/weaver/weaver.file.FileDownload?fileid=%s&download=1&ddcode=%s";
//        //使用泛微对应的该方法
//        weaver.docs.docs.util.DesUtils des = new weaver.docs.docs.util.DesUtils();
//        String ddcode = 1+ "_" + fileid;
//
//        ddcode = des.encrypt(ddcode);
//
//        return String.format(downUrl, serverIp,fileid,ddcode);
//
//    }
//
//    @WebServlet("/download_redirect")
//    public class RedirectServlet extends HttpServlet {
//        protected void ttttt(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//            String externalUrl = "http://oa.maxwell-gp.com:8090/weaver/weaver.file.FileDownload?fileid=aa9cff931181b4fa89b9f62e6daa57bbd487e88e04bd7d74dd48cf7895f79f4798433b4c282fd386d49400a99897b6cd954c6417c21db3472&download=1&ddcode=3e6a8b517b5a6e96e6b92e28c43dccd4";
//            response.sendRedirect(externalUrl);
//        }
//    }
//
//    @Path("/download_by_imagefileid_没用了")
//        @POST
//        @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_FORM_URLENCODED, "text/html; charset=UTF-8"})
//        @Produces({MediaType.APPLICATION_JSON, "text/html; charset=UTF-8"})
//        public Response download_by_imagefileid_没用了(@Context HttpServletResponse response, @Context HttpServletRequest request) throws Exception {
//
//            String url=request.getParameter("url");//http://oa.maxwell-gp.com:8090
//            String imagefileids=request.getParameter("imagefileid");
//
//            // 设置 CORS 头 为了解决浏览器跨域资源问题
//            response.setHeader("Access-Control-Allow-Origin", "*");
//            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
//            response.setHeader("Access-Control-Max-Age", "3600");
//            response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me");
//
//            String docid = Util.null2String(request.getParameter("docid"));
//            String[] docids = docid.split(",");
//            List<String> AllFileUrls = new ArrayList<>();
//
//
//            String imagefileid[]=imagefileids.split(",");
//            for( int i=0;i<imagefileid.length;i++){
//                String id=imagefileid[i];
//                String output = new DesUtils().encrypt(1 + "_"+id );
//                output=url+"/weaver/weaver.file.FileDownload?fileid="+id+"&download=1&ddcode="+output;
//                AllFileUrls.add(output);
//            }
//
//
//            if (AllFileUrls.isEmpty()) {
//                return Response.status(Response.Status.BAD_REQUEST)
//                        .entity("{\"error\":\"找不到文件\"}")
//                        .type(MediaType.APPLICATION_JSON)
//                        .build();
//            }
//
//            String jsonResponse = new Gson().toJson(Collections.singletonMap("redirectUrls", AllFileUrls));
//            // 返回所有的下载链接
//            return Response.ok(jsonResponse)
//                    .type(MediaType.APPLICATION_JSON)
//                    .build();
//        }
//
}

